/*
 * Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
 *
 * Licensed under the Mulan PSL v2.
 * You can use this file according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of the License at
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
 * FOR MORE DETAILS.
 */

#pragma once

#include "highfive/H5File.hpp"

#include "DBManager/IRepository/Input/Design/IBEADVARRepository.h"
#include "DBManager/Repository/HDF5/Base/MainlyRepositoryBase.h"
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
namespace BEADVAR
{
    struct IDENTITY
    {
        SG::DataStructure::Common::Id   BEADID    = 0;      ///<
        char                            PTYPE[8]  = { 0 };  ///<
        SG::DataStructure::Common::Id   PID       = 0;      ///<
        SG::DataStructure::Common::Real MW        = 0.0;    ///<
        SG::DataStructure::Common::Real MH        = 0.0;    ///<
        SG::DataStructure::Common::Real ANG       = 0.0;    ///<
        SG::DataStructure::Common::I64  BF        = 0;      ///<
        SG::DataStructure::Common::I64  SKIP      = 0;      ///<
        SG::DataStructure::Common::I64  DIR_LEN   = 0;      ///<
        SG::DataStructure::Common::I64  SYM_LEN   = 0;      ///<
        SG::DataStructure::Common::I64  PAT_LEN   = 0;      ///<
        SG::DataStructure::Common::Id   GRID_LEN  = 0;      ///<
        SG::DataStructure::Common::I64  DIR_POS   = 0;      ///<
        SG::DataStructure::Common::I64  SYM_POS   = 0;      ///<
        SG::DataStructure::Common::I64  PAT_POS   = 0;      ///<
        SG::DataStructure::Common::Id   GRID_POS  = 0;      ///<
        SG::DataStructure::Common::Id   DOMAIN_ID = 0;      ///<
    };
};  // namespace BEADVAR
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::BEADVAR::IDENTITY> ()
{
    HighFive::CompoundType compoundType = { { "BEADID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                            { "PTYPE", HighFive::create_datatype<char[8]> (), 1, true },
                                            { "PID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                            { "MW", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                            { "MH", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                            { "ANG", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                            { "BF", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "SKIP", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DIR_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "SYM_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "PAT_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "GRID_LEN", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                            { "DIR_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "SYM_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "PAT_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "GRID_POS", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                            { "DOMAIN_ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () } };
    return compoundType;
}

template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::BEADVARData::DIRECTIONData> ()
{
    HighFive::CompoundType ct = { { "NORM", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                  { "CID1", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                  { "N", HighFive::create_datatype<SG::DataStructure::Common::Real[3]> (), 8, true },
                                  { "XLB", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                  { "XUB", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                  { "DELXV", HighFive::create_datatype<SG::DataStructure::Common::Real> () } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::BEADVARData::GRIDSETData> ()
{
    HighFive::CompoundType ct = { { "NDGSET", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                  { "DGSET", HighFive::create_datatype<SG::DataStructure::Common::I64> () } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::BEADVARData::PATTERNData> ()
{
    HighFive::CompoundType ct = { { "CID3", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                  { "PATTY", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                  { "PATDIR", HighFive::create_datatype<SG::DataStructure::Common::I64> () } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::BEADVARData::SYMPLANEData> ()
{
    HighFive::CompoundType ct = { { "CID2", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                  { "MS1", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                  { "MS2", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                  { "MS3", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                  { "CSM", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                  { "NCSM", HighFive::create_datatype<SG::DataStructure::Common::I64> () } };
    return ct;
}
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
class BEADVARRepository : public IBEADVARRepository, public SG::DBManager::HDF5::MainlyRepositoryBase<SG::DataStructure::FEM::BEADVARData>
{
    using MainStruct    = SG::DataStructure::FEM::BEADVARData;
    using SubStructDIR  = SG::DataStructure::FEM::BEADVARData::DIRECTIONData;
    using SubStructGRID = SG::DataStructure::FEM::BEADVARData::GRIDSETData;
    using SubStructPAT  = SG::DataStructure::FEM::BEADVARData::PATTERNData;
    using SubStructSYM  = SG::DataStructure::FEM::BEADVARData::SYMPLANEData;

    using NasMainStruct = SG::DBManager::HDF5::BEADVAR::IDENTITY;

  public:
    explicit BEADVARRepository (HDF5Database& database);

    enum DATASET : std::uint8_t
    {
        MainDataset,
        IndexDataset,
        SubDataset_DIR,
        SubDataset_GRID,
        SubDataset_PAT,
        SubDataset_SYM
    };

    /**
     * @brief   : 初始化表数据
     * @author  : wuxingchuang
     */
    void InitialDatasetInfo () override;

    /**
     * @brief   : 将缓存数据转储到文件内
     */
    void DumpWriteCahce () override;

    /**
     * @brief   : 加载文件数据至缓存
     * @param   : loadIndex
     */
    std::vector<DataType>* LoadReadCache (std::size_t loadIndex) override;

    /**
     * @brief   : 对数据进行排序
     */
    void SortData () override;

    /**
     * @brief   : 获取所有数据
     */
    std::vector<MainStruct> GetAllData () override;

    /**
     * @brief   : 将自己的数据转存为nastran的数据结构
     */
    void OwnToNastran (std::vector<MainStruct>&         own,
                       std::vector<NasMainStruct>& _OUT IDENTITYData,
                       std::vector<SubStructDIR>& _OUT  subDataDIR,
                       std::vector<SubStructGRID>& _OUT subDataGRID,
                       std::vector<SubStructPAT>& _OUT  subDataPAT,
                       std::vector<SubStructSYM>& _OUT  subDataSYM,

                       bool isSort = false);

    /**
     * @brief   : 将NASTRAN的数据转存为自己的数据结构
     * @param   : IDENTITYData
     * @param   : subData
     * @param   : own
     */
    void NastranToOwn (std::vector<NasMainStruct>&   IDENTITYData,
                       std::vector<SubStructDIR>&    subDataDIR,
                       std::vector<SubStructGRID>&   subDataGRID,
                       std::vector<SubStructPAT>&    subDataPAT,
                       std::vector<SubStructSYM>&    subDataSYM,
                       std::vector<MainStruct>& _OUT own);
};
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
